package club.terroir.server.contact;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import club.terroir.server.user.Utilisateur_SS;
import club.terroir.server.util.UtilPersistence;

public class Contact_SSFactory {

	private static Contact_SSFactory instance = new Contact_SSFactory();

	/** commit l'objet Contact_SS en base de donn�e */
	public void persist(Contact_SS_ u) {
		EntityManager em = UtilPersistence.get().createEntityManager();
		try {
			em.persist(u);		
		} finally {
			em.close();
		}
	}

	public static Contact_SSFactory getInstance() {
		return instance;
	}

	public Contact_SS_ getUserByEmail(String idUser) {
		EntityManager em = UtilPersistence.get().createEntityManager();
		try {
			Query q = em.createQuery("select from "	+ Contact_SS_.class.getName() + " where email=:1");
			q.setParameter(1, idUser);
			@SuppressWarnings("unchecked")
			List<Contact_SS_>  listU =  q.getResultList();
			if (listU.size()==0){
				return null;
			}
			return listU.get(0);
		} finally {
			em.close();
		}
	}

	public List<Contact_SS_> getListContacts(Utilisateur_SS owner) {
		EntityManager em = UtilPersistence.get().createEntityManager();
		try {
			Query q = em.createQuery("select from "	+ Contact_SS_.class.getName() + " where emAilUtilisateur=:1");
			q.setParameter(1, owner.getEmail());
			//Query q = em.createQuery("select from "	+ Contact_SS.class.getName() );
			System.out.println("Contact_SSFactory getListContacts "+q);
			@SuppressWarnings("unchecked")
			List<Contact_SS_>  listU =  q.getResultList();
			System.out.println("Contact_SSFactory getListContacts listU :"+listU);
			
			return listU;
		} finally {
			em.close();
		}
	}
}
